package skripsi.GeometriAndroid;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class GameView extends SurfaceView implements OnTouchListener,SensorEventListener,SurfaceHolder.Callback{
	
    private SurfaceHolder holder;
    private GameLoopThread gameLoopThread;
    private List<Sprite> sprites = new ArrayList<Sprite>();
    private boolean isMove;
    private SensorManager sensorManager;
    private Sprite human;
    private Map mapGame;
    
	public GameView(Context context) {
		super(context);
		this.setOnTouchListener(this);
		//sensor configuration
		sensorManager=(SensorManager) this.getContext().getSystemService(Context.SENSOR_SERVICE);
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_GAME);
		// TODO Auto-generated constructor stub
		gameLoopThread = new GameLoopThread(this,10);
        holder = getHolder();
        holder.addCallback(this);
        isMove=false;
	}
	
	private void createSprites() {
		  human=createSprite(R.drawable.kadukisprite,90,90,4,3,5);
		  int[] frames={6,10,6,2};
		  human.setFrameSequence(frames);
          sprites.add(human);
          Sprite enemy=createSprite(R.drawable.sprite,120,165,1,3,5);
          int[] frames2={1,2,1,0};
		  enemy.setFrameSequence(frames2);
          sprites.add(enemy);
          //create map
          mapGame=createMap(R.drawable.map2,1,15);
          int[][] mapFrame={{0,0,0,0,0,0,0},
		        		    {0,0,0,0,0,0,0},
		        		    {0,1,1,0,0,1,1},
		        		    {0,1,1,0,0,1,1},
		        		    {0,1,1,0,0,1,1},
		        		    {0,1,1,0,0,0,0},
		        		    {0,1,1,0,0,0,0},
		        		    {0,1,1,0,0,1,1},
		        		    {0,1,1,0,0,1,1},
		        		    {0,1,0,0,0,0,0},
		        		    {0,0,0,0,0,0,0},
		        		    {0,0,0,0,0,0,0},
		        		    {0,0,0,0,0,0,0}};
          mapGame.setFrameSequence(mapFrame);
    }


    private Sprite createSprite(int resouce,int x,int y,int cols,int rows,int speed) {
          Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
          return new Sprite(this, bmp,x,y,cols,rows,speed);
    }

    private Map createMap(int resouce,int cols,int rows) {
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
        return new Map(bmp,cols,rows);
  }

    public void checkCollides(Sprite sprite){
    	if(sprite!=human && human.isCollition(sprite)){
    		Random rnd=new Random();
    		int x=rnd.nextInt(this.getWidth()-sprite.getWidth());
    		int y=rnd.nextInt(this.getHeight()-sprite.getHeight());
    		sprite.setX(x);
    		sprite.setY(y);
    	}
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
    	  canvas.drawColor(Color.BLACK);
    	  mapGame.onDraw(canvas);
    	  if(isMove){
        	  human.move();
          }
          for (Sprite sprite : sprites) {
        	  if(sprite!=human){
        		  sprite.nextFrame();
        	  }
        	  checkCollides(sprite);
        	  sprite.onDraw(canvas);
          }
    }

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		createSprites();
		
        gameLoopThread.setRunning(true);
        gameLoopThread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		gameLoopThread.setRunning(false);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
			//assign directions
			int x=(int) event.values[0];
			int y=(int) event.values[1];
			
			
			if(y<0 && Math.abs(y)>Math.abs(x) && sprites.get(0).getDirection()!=Sprite.up){//LANSCAPE:LEFT
				sprites.get(0).setDirection(Sprite.up);
				int[] frames={6,10,6,2};
				sprites.get(0).setFrameSequence(frames);
		
			}else if(y>0 && Math.abs(y)>Math.abs(x)&& sprites.get(0).getDirection()!=Sprite.down){//LANSCAPE:RIGHT
				sprites.get(0).setDirection(Sprite.down);
				int[] frames={5,9,5,1};
				sprites.get(0).setFrameSequence(frames);
			
			}else if(x<0 && Math.abs(x)>Math.abs(y)&& sprites.get(0).getDirection()!=Sprite.right){//LANSCAPE:UP
				sprites.get(0).setDirection(Sprite.right);
				int[] frames={4,8,4,0};
				sprites.get(0).setFrameSequence(frames);
			
			}else if(x>0 && Math.abs(x)>Math.abs(y)&& sprites.get(0).getDirection()!=Sprite.left){//LANSCAPE:DOWN
				sprites.get(0).setDirection(Sprite.left);
				int[] frames={7,11,7,3};
				sprites.get(0).setFrameSequence(frames);
		
			}
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
int evCode=event.getAction();
		
		if(evCode==MotionEvent.ACTION_DOWN){
			isMove=true;
		}else if(evCode==MotionEvent.ACTION_UP){
			isMove=false;
		}
		return true;
	}

}
